

****************************************************************
************ Decode & Reorder Vars
****************************************************************

****** Decode vars & reorder 

****** Generate list of vars to decode 
ds, has(vall) 
loc dcv1 `r(varlist)'
ds, has(type long)
loc dcv2 `r(varlist)'
loc decodevars: list dcv1 & dcv2
di "`decodevars'"


****** Reorder
order personid firstname lastname appyear corpsyear matriculatedregion admitted matriculated4 dispositionstep ///
		homephone workphone cellphone appethnicity gender undergraduateuniversity dateofbirth degreeconferreddate degreedate
		zscore 
		TFA_Master_UID ///
		First_Name Last_Name Calculated_Prospect_Type_cleaned Disposition_Step App_Year ///
		Current_Address_Address2 Current_Address_City Current_Address_Country Current_Address_State Current_Address_Zip ///
		Permanent_Address_Address2 Permanent_Address_City Permanent_Address_Country Permanent_Address_State Permanent_Address_Zip ///
		state_ca state_pa state_ca_clean state_pa_clean state_uni ///
		inst_multi campus_multi no_campuses uni_state_source state_st state_mr state_ac1 state_ac2 ///
		FirstName_tfa LastName_tfa Sex_tfa BirthYear_tfa ///
		LandLine_AreaCode_tfa LandLine_Number_tfa CellPhone_AreaCode_tfa CellPhone_Number_tfa Current_Address_Zip_tfa Permanent_Address_Zip_tfa ///
		vf_match_state_ca_flag ///
		vf_match_state_uni_flag vf_match_state_st_flag ///
		vf_match_state_ca ///
		vf_match_state_uni vf_match_state_st vf_match_state_mr ///
		total_sources_all total_sources_sub total_sources_nomat total_sources_app ///
		state_vf_matched /// 	
		DT_ID DT_RegID StateVoterID State NamePrefix FirstName_vf MiddleName LastName_vf NameSuffix Sex_vf BirthYear_vf ///
		RegisteredParty DTCalcParty RegisteredParty_RollUp SelfReportedDemographic ModeledEthnicity Race ///
		CountyFIPS PrecinctNumber PrecinctName RegistrationAddress2 RegistrationAddressZip5_vf RegistrationAddressLatitude ///
		RegistrationAddressLongitude MailingAddress2 MailingAddressZip5_vf ///
		LandLine_AreaCode_vf LandLine_Number_vf CellPhone_AreaCode_vf CellPhone_Number_vf ///
		CellPhone_SourceCode CellPhone_MatchLevel CellPhone_ReliabilityCode ///
		LastActiveDate RegistrationDate VoterStatus PermanentAbsenteeFlag ///
		VH16G VH16P VH16PP VH15G VH15P VH14G VH14P VH13G VH13P VH12G VH12P VH12PP VH11G VH11P VH10G VH10P VH09G VH09P VH08G VH08P VH08PP ///
		VH07G VH07P VH06G VH06P VH05G VH05P VH04G VH04P VH04PP VH03G VH03P VH02G VH02P state_vf_matched matched_to_vf ///no_of_matches


****************************************************************
************ Merge with Original TFA Data
****************************************************************		
		
****** Merge with Original TFA Data:
merge m:1 personid using "..\Data\tfa_data.dta", force
keep if _merge==3
drop _merge

****** (NEW) Add citizenship variable from survey:
drop uscitizen
merge 1:1 personid using "..\Data\2007.2015 Apps Submitted Citizen Status.dta"
keep if _merge==3
drop _merge


****************************************************************
************ Clean & Impute DOB 
****************************************************************

****** Trim dob and degree date
replace dateofbirth=trim(dateofbirth)
replace degreedate=trim(degreedate)

****** Code empty dob as missing
gen missing_dob=1 if (BirthYear_tfa=="")
gen missing_both=1 if dateofbirth=="" & degreedate==""
gen missing_both2=1 if dateofbirth=="" & BirthYear_tfa==""
gen missing_both3=1 if dateofbirth~="" & BirthYear_tfa==""
replace missing_both3=0 if missing_both3==.

****** Create var with degree year 
gen degree_year=substr(degreedate, -2, 2)
destring degree_year, replace
replace degree_year=degree_year+2000 if degree_year<=15
replace degree_year=degree_year+1900 if degree_year>15 & degree_year<2000

****** Create DOB variable 
gen BirthYear = BirthYear_tfa

****** Create imputed dob var (Updated for survey only)
gen impute_dob = 0 
destring DegYear DegMonth DegDay, replace
replace impute_dob = 1 if (DegYear != .) 
gen bYdeg = DegYear - 22


****************************************************************
************ Election Dates
****************************************************************

****** Per Julia's R file... 
gen elec_2002 = date("11/5/2002", "MDY")
gen elec_2003 = date("11/4/2003", "MDY")
gen elec_2004 = date("11/2/2004", "MDY")
gen elec_2005 = date("11/8/2005", "MDY")
gen elec_2006 = date("11/7/2006", "MDY")
gen elec_2007 = date("11/6/2007", "MDY")
gen elec_2008 = date("11/4/2008", "MDY")
gen elec_2009 = date("11/3/2009", "MDY")
gen elec_2010 = date("11/2/2010", "MDY")
gen elec_2011 = date("11/8/2011", "MDY")
gen elec_2012 = date("11/6/2012", "MDY")
gen elec_2013 = date("11/5/2013", "MDY")
gen elec_2014 = date("11/4/2014", "MDY")
gen elec_2015 = date("11/3/2015", "MDY")
gen elec_2016 = date("11/8/2016", "MDY")


****************************************************************
************ Eligibility & TFA-Status by Election
****************************************************************

if inlist(${dedup_num}, 30,31) {
	destring matriculated4 corpsyear, replace
}

destring BirthYear, replace


forvalues y = 2002(1)2016 {

	* Create var with person's bday in elec year 
	gen bday_eY = mdy(7, 1, `y')

	* Generate eligibility var 
	gen elig`y' = .
	
	* Eligible if 18 at election 
	replace elig`y' = 0 if `y' <= BirthYear+17
	replace elig`y' = 0 if (bday_eY > elec_`y') & (`y' == BirthYear+18)
	replace elig`y' = 1 if (bday_eY <= elec_`y') & (`y' == BirthYear+18)
	replace elig`y' = 1 if `y' > BirthYear+18
	
	* Eligible if 18 at election (IMPUTED BDAY)
	replace elig`y' = 1 if (impute_dob == 1) & (`y'-bYdeg > 18)
	
	* Whether post graduation at elec 
	gen postgrad_`y' = 0
	replace postgrad_`y' = 1 if (new_degreedate < elec_`y')
	
	* Whether pre or post TFA application at elec 
	gen preAPP_`y' = 0
	replace preAPP_`y' = 1 if (appyear > `y')
	
	gen postAPP_`y' = 0
	replace postAPP_`y' = 1 if (appyear <= `y')
	
	* Whether post TFA years at elec (for rejected applicants, 2 years after application) 
	gen postTFA_`y' = .
	replace postTFA_`y' = 0 if (appyear+2 > `y') & (matriculated4 == 0)
	replace postTFA_`y' = 1 if (appyear+2 <= `y') & (matriculated4 == 0)
	replace postTFA_`y' = 0 if (corpsyear+2 > `y') & (matriculated4 == 1)
	replace postTFA_`y' = 1 if (corpsyear+2 <= `y')	& (matriculated4 == 1)
	
	* Whether post 1 year of TFA at elec (for rejected applicants, 1 years after application) 
	gen post1Y_`y' = .
	replace post1Y_`y' = 0 if (appyear+1 > `y') & (matriculated4 == 0)
	replace post1Y_`y' = 1 if (appyear+1 <= `y') & (matriculated4 == 0)
	replace post1Y_`y' = 0 if (corpsyear+1 > `y') & (matriculated4 == 1)
	replace post1Y_`y' = 1 if (corpsyear+1 <= `y') & (matriculated4 == 1)		
	
	* Rename var & drop extra vars 
	drop bday_eY
}
*


****************************************************************
************ Recode Election / Vote Values 
****************************************************************

****** Presidential-Year & Midterm Elections
local yearabb2to16 `" "02" "04" "06" "08" "10" "12" "14" "16" "'
foreach y of local yearabb2to16 {
	destring VH`y'G, replace
	replace VH`y'G = 1 if (VH`y'G >= 1) & (VH`y'G != .)
	replace VH`y'G = 0 if (VH`y'G == .) & (elig20`y' == 1)
	replace VH`y'G = . if (elig20`y' == 0)
}

****** Offcycle Elections
local yearabb2to16 `" "03" "05" "07" "09" "11" "13" "15" "'
foreach y of local yearabb2to16 {
	destring VH`y'G, replace
	replace VH`y'G = 1 if (VH`y'G >= 1) & (VH`y'G != .)
	replace VH`y'G = 0 if (VH`y'G == .) & (elig20`y' == 1)
	replace VH`y'G = . if (elig20`y' == 0)
}
*

****************************************************************
************ Registration Vars 
****************************************************************

****** Convert to date (if reg date is string)
capture confirm str variable RegistrationDate
	if !_rc {
	   gen temp = date(RegistrationDate, "YMD")
	   drop RegistrationDate
	   gen RegistrationDate = temp
	   drop temp  
	}

****** Recode registration
* Registered
gen registered=1 if (RegistrationDate != .)
replace registered=0 if (RegistrationDate == .)
* Not Registered 
gen not_registered=1 if RegistrationDate==.
replace not_registered=0 if RegistrationDate~=.

****** Create Party Registration Variables
if( inlist(${dedup_num}, 3,4,5,6) ) {

	****** Subset Data (State)
	if (${dedup_num}==3) global stateVar "vf_match_state_cell"
	if (${dedup_num}==4) global stateVar "vf_match_state_uni"
	if (${dedup_num}==5) global stateVar "vf_match_state_ca"
	if (${dedup_num}==6) global stateVar "vf_match_state_st"

	****** Party Reg State 
	// Note: based on match1, dedup3; ran 'tab state_pa RegisteredParty'; identified states with D=0 
	gen party_available=1 
	replace party_available=0 if inlist(${stateVar}, "AL", "GA" ,"HI" ,"IL", "IN")
	replace party_available=0 if inlist(${stateVar}, "MI", "MN", "MO", "MS", "MT")
	replace party_available=0 if inlist(${stateVar}, "SC", "TN", "TX", "VA", "VT")
	replace party_available=0 if inlist(${stateVar}, "WA", "WI")

	****** Political Party
	* Dems
	gen registered_democrat=1 if RegisteredParty=="D"
	replace registered_democrat=0 if RegisteredParty~="D"
	replace registered_democrat=. if party_available==0
	* Reps
	gen registered_republican=1 if RegisteredParty=="R"
	replace registered_republican=0 if RegisteredParty~="R"
	replace registered_republican=. if party_available==0 
	* Any Party 
	gen registered_party=1 if registered==1 & RegisteredParty!="" & RegisteredParty!="I"
	replace registered_party=0 if registered==1 | RegisteredParty=="" | RegisteredParty=="I"
	replace registered_party=. if party_available==0
	* No Party 
	gen registered_no_party=1 if registered==1 & (RegisteredParty=="" | RegisteredParty=="I")
	replace registered_no_party=0 if registered!=1 | (RegisteredParty!="" & RegisteredParty!="I")
	replace registered_no_party=. if party_available==0
	* Major Party 
	gen registered_DorR =1 if RegisteredParty=="R" | RegisteredParty=="D"
	replace registered_DorR=0 if RegisteredParty~="R" & RegisteredParty~="D"
	replace registered_DorR=. if party_available==0 
}


****************************************************************
************ Voting pre/post-APP and post-TFA (2004-2015) 
****************************************************************

****** Presidential-Year & Midterm
* Select years
local setyears `" "04" "06" "08" "10" "12" "14" "'
* Loop 
foreach p of newlist postAPP postTFA preAPP post1Y {
	* Total votes (when eligible)
	gen sum_votes_`p' = .
	foreach y of local setyears {
		replace sum_votes_`p' = 0 if VH`y'G != .
	}
	foreach y of local setyears {
		replace sum_votes_`p' = sum_votes_`p' + (`p'_20`y' * VH`y'G) if VH`y'G != .
	}
	* Total elec (when eligible)
	gen sum_elec_`p' = 0
	foreach y of local setyears {
		replace sum_elec_`p' = sum_elec_`p' + (`p'_20`y' * elig20`y')
	}
	* Summarize ever_vote
	gen ever_vote_`p' = . 
	replace ever_vote_`p' = 0 if sum_votes_`p' == 0
	replace ever_vote_`p' = 1 if (sum_votes_`p' > 0) & (sum_votes_`p' != .)
	replace ever_vote_`p' = . if sum_elec_`p' == 0
	* Summarize proportion
	gen prop_vote_`p' = sum_votes_`p' / sum_elec_`p'
	// replace prop_vote_`p' = 0 if sum_elec_`p' == 0
	// replace prop_vote_`p' = 0 if sum_votes_`p' == .		
}

****** Presidential Elections ONLY
* Select years
local setyears `" "04" "08" "12" "'
* Loop 
foreach p of newlist postAPP postTFA preAPP post1Y {
	* Total votes (when eligible)
	gen sum_votes_`p'_pres = .
	foreach y of local setyears {
		replace sum_votes_`p'_pres = 0 if VH`y'G != .
	}
	foreach y of local setyears {
		replace sum_votes_`p'_pres = sum_votes_`p'_pres + (`p'_20`y' * VH`y'G) if VH`y'G != .
	}
	* Total elec (when eligible)
	gen sum_elec_`p'_pres = 0
	foreach y of local setyears {
		replace sum_elec_`p'_pres = sum_elec_`p'_pres + (`p'_20`y' * elig20`y')
	}
	* Summarize ever_vote
	gen ever_vote_`p'_pres = . 
	replace ever_vote_`p'_pres = 0 if sum_votes_`p'_pres == 0
	replace ever_vote_`p'_pres = 1 if (sum_votes_`p'_pres > 0) & (sum_votes_`p'_pres != .)
	replace ever_vote_`p'_pres = . if sum_elec_`p'_pres == 0
	* Summarize proportion
	gen prop_vote_`p'_pres = sum_votes_`p'_pres / sum_elec_`p'_pres	
}

****** Midterm Elections ONLY
* Select years
local setyears `" "06" "10" "14" "'
* Loop 
foreach p of newlist postAPP postTFA preAPP post1Y {
	* Total votes (when eligible)
	gen sum_votes_`p'_mid = .
	foreach y of local setyears {
		replace sum_votes_`p'_mid = 0 if VH`y'G != .
	}
	foreach y of local setyears {
		replace sum_votes_`p'_mid = sum_votes_`p'_mid + (`p'_20`y' * VH`y'G) if VH`y'G != .
	}
	* Total elec (when eligible)
	gen sum_elec_`p'_mid = 0
	foreach y of local setyears {
		replace sum_elec_`p'_mid = sum_elec_`p'_mid + (`p'_20`y' * elig20`y')
	}
	* Summarize ever_vote
	gen ever_vote_`p'_mid = . 
	replace ever_vote_`p'_mid = 0 if sum_votes_`p'_mid == 0
	replace ever_vote_`p'_mid = 1 if (sum_votes_`p'_mid > 0) & (sum_votes_`p'_mid != .)
	replace ever_vote_`p'_mid = . if sum_elec_`p'_mid == 0
	* Summarize proportion
	gen prop_vote_`p'_mid = sum_votes_`p'_mid / sum_elec_`p'_mid		
}

****************************************************************
************ Voting pre/post-APP and post-TFA (2008-2015) 
****************************************************************


****** Presidential-Year & Midterm
* Select years
local setyears `" "08" "10" "12" "14" "'
* Loop
foreach p of newlist postAPP postTFA preAPP post1Y {
	* Total votes (when eligible)
	gen sum_votes_`p'_08to5 = .
	foreach y of local setyears {
		replace sum_votes_`p'_08to5 = 0 if VH`y'G != .
	}
	foreach y of local setyears {
		replace sum_votes_`p'_08to5 = sum_votes_`p'_08to5 + (`p'_20`y' * VH`y'G) if VH`y'G != .
	}
	* Total elec (when eligible)
	gen sum_elec_`p'_08to5 = 0
	foreach y of local setyears {
		replace sum_elec_`p'_08to5 = sum_elec_`p'_08to5 + (`p'_20`y' * elig20`y')
	}
	* Summarize ever_vote
	gen ever_vote_`p'_08to5 = . 
	replace ever_vote_`p'_08to5 = 0 if sum_votes_`p'_08to5 == 0
	replace ever_vote_`p'_08to5 = 1 if (sum_votes_`p'_08to5 > 0) & (sum_votes_`p'_08to5 != .)
	replace ever_vote_`p'_08to5 = . if sum_elec_`p'_08to5 == 0	
	* Summarize proportion
	gen prop_vote_`p'_08to5 = sum_votes_`p'_08to5 / sum_elec_`p'_08to5

}

****** Presidential Elections ONLY
* Select years
local setyears `" "08" "12" "'
* Loop
foreach p of newlist postAPP postTFA preAPP post1Y {
	* Total votes (when eligible)
	gen sum_votes_`p'_pres_08to5 = .
	foreach y of local setyears {
		replace sum_votes_`p'_pres_08to5 = 0 if VH`y'G != .
	}
	foreach y of local setyears {
		replace sum_votes_`p'_pres_08to5 = sum_votes_`p'_pres_08to5 + (`p'_20`y' * VH`y'G) if VH`y'G != .
	}
	* Total elec (when eligible)
	gen sum_elec_`p'_pres_08to5 = 0
	foreach y of local setyears {
		replace sum_elec_`p'_pres_08to5 = sum_elec_`p'_pres_08to5 + (`p'_20`y' * elig20`y')
	}
	* Summarize ever_vote
	gen ever_vote_`p'_pres_08to5 = . 
	replace ever_vote_`p'_pres_08to5 = 0 if sum_votes_`p'_pres_08to5 == 0
	replace ever_vote_`p'_pres_08to5 = 1 if (sum_votes_`p'_pres_08to5 > 0) & (sum_votes_`p'_pres_08to5 != .)
	replace ever_vote_`p'_pres_08to5 = . if sum_elec_`p'_pres_08to5 == 0	
	* Summarize proportion
	gen prop_vote_`p'_pres_08to5 = sum_votes_`p'_pres_08to5 / sum_elec_`p'_pres_08to5
	
}

****** Midterm Elections ONLY
* Select years
local setyears `" "10" "14" "'
* Loop
foreach p of newlist postAPP postTFA preAPP post1Y {
	* Total votes (when eligible)
	gen sum_votes_`p'_mid_08to5 = .
	foreach y of local setyears {
		replace sum_votes_`p'_mid_08to5 = 0 if VH`y'G != .
	}
	foreach y of local setyears {
		replace sum_votes_`p'_mid_08to5 = sum_votes_`p'_mid_08to5 + (`p'_20`y' * VH`y'G) if VH`y'G != .
	}
	* Total elec (when eligible)
	gen sum_elec_`p'_mid_08to5 = 0
	foreach y of local setyears {
		replace sum_elec_`p'_mid_08to5 = sum_elec_`p'_mid_08to5 + (`p'_20`y' * elig20`y')
	}
	* Summarize ever_vote
	gen ever_vote_`p'_mid_08to5 = . 
	replace ever_vote_`p'_mid_08to5 = 0 if sum_votes_`p'_mid_08to5 == 0
	replace ever_vote_`p'_mid_08to5 = 1 if (sum_votes_`p'_mid_08to5 > 0) & (sum_votes_`p'_mid_08to5 != .)
	replace ever_vote_`p'_mid_08to5 = . if sum_elec_`p'_mid_08to5 == 0	
	* Summarize proportion
	gen prop_vote_`p'_mid_08to5 = sum_votes_`p'_mid_08to5 / sum_elec_`p'_mid_08to5

}



****************************************************************
************ Voting pre/post-APP and post-TFA (2010-2015) 
****************************************************************


****** Presidential-Year & Midterm
* Select years
local setyears `" "10" "12" "14" "'
* Loop
foreach p of newlist postAPP postTFA preAPP post1Y {
	* Total votes (when eligible)
	gen sum_votes_`p'_10to5 = .
	foreach y of local setyears {
		replace sum_votes_`p'_10to5 = 0 if VH`y'G != .
	}
	foreach y of local setyears {
		replace sum_votes_`p'_10to5 = sum_votes_`p'_10to5 + (`p'_20`y' * VH`y'G) if VH`y'G != .
	}
	* Total elec (when eligible)
	gen sum_elec_`p'_10to5 = 0
	foreach y of local setyears {
		replace sum_elec_`p'_10to5 = sum_elec_`p'_10to5 + (`p'_20`y' * elig20`y')
	}
	* Summarize ever_vote
	gen ever_vote_`p'_10to5 = . 
	replace ever_vote_`p'_10to5 = 0 if sum_votes_`p'_10to5 == 0
	replace ever_vote_`p'_10to5 = 1 if (sum_votes_`p'_10to5 > 0) & (sum_votes_`p'_10to5 != .)
	replace ever_vote_`p'_10to5 = . if sum_elec_`p'_10to5 == 0	
	* Summarize proportion
	gen prop_vote_`p'_10to5 = sum_votes_`p'_10to5 / sum_elec_`p'_10to5	
}

****** Presidential Elections ONLY
* Select years
local setyears `" "12" "'
* Loop
foreach p of newlist postAPP postTFA preAPP post1Y {
	* Total votes (when eligible)
	gen sum_votes_`p'_pres_10to5 = .
	foreach y of local setyears {
		replace sum_votes_`p'_pres_10to5 = 0 if VH`y'G != .
	}
	foreach y of local setyears {
		replace sum_votes_`p'_pres_10to5 = sum_votes_`p'_pres_10to5 + (`p'_20`y' * VH`y'G) if VH`y'G != .
	}
	* Total elec (when eligible)
	gen sum_elec_`p'_pres_10to5 = 0
	foreach y of local setyears {
		replace sum_elec_`p'_pres_10to5 = sum_elec_`p'_pres_10to5 + (`p'_20`y' * elig20`y')
	}
	* Summarize ever_vote
	gen ever_vote_`p'_pres_10to5 = . 
	replace ever_vote_`p'_pres_10to5 = 0 if sum_votes_`p'_pres_10to5 == 0
	replace ever_vote_`p'_pres_10to5 = 1 if (sum_votes_`p'_pres_10to5 > 0) & (sum_votes_`p'_pres_10to5 != .)
	replace ever_vote_`p'_pres_10to5 = . if sum_elec_`p'_pres_10to5 == 0	
	* Summarize proportion
	gen prop_vote_`p'_pres_10to5 = sum_votes_`p'_pres_10to5 / sum_elec_`p'_pres_10to5
		
}

****** Midterm Elections ONLY
* Select years
local setyears `" "10" "14" "'
* Loop
foreach p of newlist postAPP postTFA preAPP post1Y {
	* Total votes (when eligible)
	gen sum_votes_`p'_mid_10to5 = .
	foreach y of local setyears {
		replace sum_votes_`p'_mid_10to5 = 0 if VH`y'G != .
	}
	foreach y of local setyears {
		replace sum_votes_`p'_mid_10to5 = sum_votes_`p'_mid_10to5 + (`p'_20`y' * VH`y'G) if VH`y'G != .
	}
	* Total elec (when eligible)
	gen sum_elec_`p'_mid_10to5 = 0
	foreach y of local setyears {
		replace sum_elec_`p'_mid_10to5 = sum_elec_`p'_mid_10to5 + (`p'_20`y' * elig20`y')
	}
	* Summarize ever_vote
	gen ever_vote_`p'_mid_10to5 = . 
	replace ever_vote_`p'_mid_10to5 = 0 if sum_votes_`p'_mid_10to5 == 0
	replace ever_vote_`p'_mid_10to5 = 1 if (sum_votes_`p'_mid_10to5 > 0) & (sum_votes_`p'_mid_10to5 != .)
	replace ever_vote_`p'_mid_10to5 = . if sum_elec_`p'_mid_10to5 == 0	
	* Summarize proportion
	gen prop_vote_`p'_mid_10to5 = sum_votes_`p'_mid_10to5 / sum_elec_`p'_mid_10to5

}




****************************************************************
************ Voting pre/post-APP and post-TFA (2012-2015) 
****************************************************************

****** Presidential-Year & Midterm
* Select years
local setyears `" "12" "14" "'
* Loop
foreach p of newlist postAPP postTFA preAPP post1Y {
	* Total votes (when eligible)
	gen sum_votes_`p'_12to5 = .
	foreach y of local setyears {
		replace sum_votes_`p'_12to5 = 0 if VH`y'G != .
	}
	foreach y of local setyears {
		replace sum_votes_`p'_12to5 = sum_votes_`p'_12to5 + (`p'_20`y' * VH`y'G) if VH`y'G != .
	}
	* Total elec (when eligible)
	gen sum_elec_`p'_12to5 = 0
	foreach y of local setyears {
		replace sum_elec_`p'_12to5 = sum_elec_`p'_12to5 + (`p'_20`y' * elig20`y')
	}
	* Summarize ever_vote
	gen ever_vote_`p'_12to5 = . 
	replace ever_vote_`p'_12to5 = 0 if sum_votes_`p'_12to5 == 0
	replace ever_vote_`p'_12to5 = 1 if (sum_votes_`p'_12to5 > 0) & (sum_votes_`p'_12to5 != .)
	replace ever_vote_`p'_12to5 = . if sum_elec_`p'_12to5 == 0	
	* Summarize proportion
	gen prop_vote_`p'_12to5 = sum_votes_`p'_12to5 / sum_elec_`p'_12to5	
}

****** Presidential Elections ONLY
* Select years
local setyears `" "12" "'
* Loop
foreach p of newlist postAPP postTFA preAPP post1Y {
	* Total votes (when eligible)
	gen sum_votes_`p'_pres_12to5 = .
	foreach y of local setyears {
		replace sum_votes_`p'_pres_12to5 = 0 if VH`y'G != .
	}
	foreach y of local setyears {
		replace sum_votes_`p'_pres_12to5 = sum_votes_`p'_pres_12to5 + (`p'_20`y' * VH`y'G) if VH`y'G != .
	}
	* Total elec (when eligible)
	gen sum_elec_`p'_pres_12to5 = 0
	foreach y of local setyears {
		replace sum_elec_`p'_pres_12to5 = sum_elec_`p'_pres_12to5 + (`p'_20`y' * elig20`y')
	}
	* Summarize ever_vote
	gen ever_vote_`p'_pres_12to5 = . 
	replace ever_vote_`p'_pres_12to5 = 0 if sum_votes_`p'_pres_12to5 == 0
	replace ever_vote_`p'_pres_12to5 = 1 if (sum_votes_`p'_pres_12to5 > 0) & (sum_votes_`p'_pres_12to5 != .)
	replace ever_vote_`p'_pres_12to5 = . if sum_elec_`p'_pres_12to5 == 0	
	* Summarize proportion
	gen prop_vote_`p'_pres_12to5 = sum_votes_`p'_pres_12to5 / sum_elec_`p'_pres_12to5
		
}

****** Midterm Elections ONLY
* Select years
local setyears `" "14" "'
* Loop
foreach p of newlist postAPP postTFA preAPP post1Y {
	* Total votes (when eligible)
	gen sum_votes_`p'_mid_12to5 = .
	foreach y of local setyears {
		replace sum_votes_`p'_mid_12to5 = 0 if VH`y'G != .
	}
	foreach y of local setyears {
		replace sum_votes_`p'_mid_12to5 = sum_votes_`p'_mid_12to5 + (`p'_20`y' * VH`y'G) if VH`y'G != .
	}
	* Total elec (when eligible)
	gen sum_elec_`p'_mid_12to5 = 0
	foreach y of local setyears {
		replace sum_elec_`p'_mid_12to5 = sum_elec_`p'_mid_12to5 + (`p'_20`y' * elig20`y')
	}
	* Summarize ever_vote
	gen ever_vote_`p'_mid_12to5 = . 
	replace ever_vote_`p'_mid_12to5 = 0 if sum_votes_`p'_mid_12to5 == 0
	replace ever_vote_`p'_mid_12to5 = 1 if (sum_votes_`p'_mid_12to5 > 0) & (sum_votes_`p'_mid_12to5 != .)
	replace ever_vote_`p'_mid_12to5 = . if sum_elec_`p'_mid_12to5 == 0	
	* Summarize proportion
	gen prop_vote_`p'_mid_12to5 = sum_votes_`p'_mid_12to5 / sum_elec_`p'_mid_12to5

}


